package com.education.controller.backend;

import com.education.annotation.AuthorityAnnotation;
import com.education.common.Const;
import com.education.pojo.Admin;
import com.education.pojo.LoginConut;
import com.education.service.IManageUserService;
import com.education.vo.MonthWeekTodayYesterDayVo;
import com.education.vo.TotalVo;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author 林心晨
 * @Title:
 * @Package
 * @Description:    管理系統，用戶接口
 * @date create in 4.18  17:05
 */
@Controller
@RequestMapping("manage/")
public class AdminManageController {
    @Autowired
    private IManageUserService iManageUserService ;

    /**
     * 如果用户已经登录并再次尝试转到登录页面，将被重定向到列表页面。
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String loginPage(){
        return "login" ;
    }

    /**
     * 管理系统，用户登录接口
     * @param session
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> login(HttpSession session , HttpServletResponse response, String username, String password){     //Model是request级别
        Map<String,Object> map ;
        if (username == null || password == null) {
            map = Maps.newHashMap();
            map.put("status",103);
            return map ;
        }
        Admin admin = iManageUserService.login(username, password);
        if (admin != null){
            session.setAttribute(Const.CURRENT_Admin,admin);
           /* Cookie cookie = new Cookie("JSESSIONID", sessionId);
            cookie.setPath("/");
            response.addCookie(cookie);*/
            map = Maps.newHashMap();
            map.put("status",100);
            return map ;
        }
        map = Maps.newHashMap();
        map.put("status",101);
        return map;
    }


    /**
     * 返回到主页面
     * @param model
     * @return
     */
    @RequestMapping(value = "indexPage",method = RequestMethod.GET)
    public ModelAndView indexPage(ModelAndView model,HttpSession session){
        Admin currentAdmin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (currentAdmin == null){
            model.setViewName("redirect:/manage/login");
            return model ;
        }
        List rootNodeAll = iManageUserService.getRootNodeAll();     //不再做权限限制。不查询所有，在业务层做判断，是0的时候就查询所有。是1的时候就查询出1对应的fid
        model.addObject("rootNodeAll",rootNodeAll);
        model.setViewName("index");
        return model ;
    }

    /**
     * 返回桌面页面
     * @param model
     * @return
     */
    @AuthorityAnnotation(authority = 0)
    @RequestMapping(value = "welcomePage",method = RequestMethod.GET)
    public ModelAndView welcomePage(ModelAndView model,HttpSession session){
        Admin currentAdmin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (currentAdmin == null){
            model.addObject("msg","管理员未登录");
            return model ;
        }
        TotalVo totalVo = iManageUserService.calculateTotal();
        LoginConut loginConut = iManageUserService.loginCount(currentAdmin.getaId());
        model.addObject("loginCount",loginConut);
        model.addObject("totalVo",totalVo);
        MonthWeekTodayYesterDayVo monthWeekTodayYesterDayVo = iManageUserService.categoryAddCount();
        model.addObject("muchVo",monthWeekTodayYesterDayVo);
        model.setViewName("welcome");
        return model ;
    }

    @RequestMapping(value = "logout",method = RequestMethod.POST)
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }

    /**
     * 验证用户名接口是否重复
     * @param username
     * @return
     */
    @AuthorityAnnotation(authority = 0)
    @RequestMapping(value = "valid_username",method = RequestMethod.GET)
    @ResponseBody
    public boolean validUsername(@RequestParam(value = "username")String username){
        if (StringUtils.isBlank(username)){
            return false ;
        }
        return iManageUserService.validUsername(username);
    }


}
